/*********************************************************************************
 *      Copyright:  (C) 2020 makun<1394987689@qq.com>
 *                  All rights reserved.
 *
 *       Filename:  sigaction.c
 *    Description:  This file 
 *                 
 *        Version:  1.0.0(2020年03月20日)
 *         Author:  makun <1394987689@qq.com>
 *      ChangeLog:  1, Release initial version on "2020年03月20日 14时19分06秒"
 *                 
 ********************************************************************************/
#include <stdio.h>
#include <signal.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>

void sig_usr(int signum)
{

    if( SIGUSR1 == signum )
    {
        printf("SIGUSR1 signal detected\n");
    }
    else if( SIGUSR2 == signum )
    {
        printf("SIGUSR2 signal detected\n");
        
    }
}

    

int main (int argc, char **argv)
{
    char    buf[512];
    int     n;
    
    struct  sigaction   sa_usr; 
    sa_usr.sa_flags =0;
    sa_usr.sa_handler = sig_usr;    //信号处理函数

    sigaction(SIGUSR1, &sa_usr, 0);
    sigaction(SIGUSR2, &sa_usr, 0);


     printf("My PID is %d\n", getpid());
         
     while(1)
     {
         if((n = read(STDIN_FILENO, buf, 511)) == -1)
         {
             if(errno == EINTR)
             {
                 printf("read is interrupted by signal\n");
             }
         }
         else
         {
             buf[n] = '\0';
             printf("%d bytes read: %s\n", n, buf);
         }
     }

     return 0;
}
